package com.sansecy.customview.view.calendar.calendarhelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description CalendarUtil
 * @Date 2019-08-20 22:19
 * @Created by caoyu
 */
public class CalendarUtil {
    private static final long ONE_DAY = 1000 * 3600 * 24;
    /**
     * 运算 calendarModel1 - calendarModel2
     * test Pass
     *
     * @param calendarModel1 calendarModel1
     * @param calendarModel2 calendarModel2
     * @return calendarModel1 - calendarModel2
     */
    static int differ(CalendarModel calendarModel1, CalendarModel calendarModel2) {
        if (calendarModel1 == null) {
            return Integer.MIN_VALUE;
        }
        if (calendarModel2 == null) {
            return Integer.MAX_VALUE;
        }
        java.util.Calendar date = java.util.Calendar.getInstance();

        date.set(calendarModel1.getYear(), calendarModel1.getMonth() - 1, calendarModel1.getDay());//

        long startTimeMills = date.getTimeInMillis();//获得起始时间戳

        date.set(calendarModel2.getYear(), calendarModel2.getMonth() - 1, calendarModel2.getDay());//

        long endTimeMills = date.getTimeInMillis();//获得结束时间戳

        return (int) ((startTimeMills - endTimeMills) / ONE_DAY);
    }

    /**
     * 判断一个日期是否是周末，即周六日
     *
     * @param calendarModel calendarModel
     * @return 判断一个日期是否是周末，即周六日
     */
    static boolean isWeekend(CalendarModel calendarModel) {
        int week = getWeekFormCalendar(calendarModel);
        return week == 0 || week == 6;
    }

    /**
     * 获取某个日期是星期几
     * 测试通过
     *
     * @param calendarModel 某个日期
     * @return 返回某个日期是星期几
     */
    static int getWeekFormCalendar(CalendarModel calendarModel) {
        java.util.Calendar date = java.util.Calendar.getInstance();
        date.set(calendarModel.getYear(), calendarModel.getMonth() - 1, calendarModel.getDay());
        return date.get(java.util.Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 是否是闰年
     *
     * @param year year
     * @return 是否是闰年
     */
    static boolean isLeapYear(int year) {
        return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }
    /**
     * DAY_OF_WEEK return  1  2  3 	4  5  6	 7，偏移了一位
     * 获取日期所在月视图对应的起始偏移量
     * Test pass
     *
     * @param year      年
     * @param month     月
     * @param weekStart 周起始
     * @return 获取日期所在月视图对应的起始偏移量 the start diff with MonthView
     */
    static int getMonthViewStartDiff(int year, int month, int weekStart) {
        java.util.Calendar date = java.util.Calendar.getInstance();
        date.set(year, month - 1, 1);
        int week = date.get(java.util.Calendar.DAY_OF_WEEK);
        if (weekStart == CalendarConstant.WEEK_START_WITH_SUN) {
            return week - 1;
        }
        if (weekStart == CalendarConstant.WEEK_START_WITH_MON) {
            return week == 1 ? 6 : week - weekStart;
        }
        return week == CalendarConstant.WEEK_START_WITH_SAT ? 0 : week;
    }

    /**
     * 获取某月的天数
     *
     * @param year  年
     * @param month 月
     * @return 某月的天数
     */
    static int getMonthDaysCount(int year, int month) {
        int count = 0;
        //判断大月份
        if (month == 1 || month == 3 || month == 5 || month == 7
                || month == 8 || month == 10 || month == 12) {
            count = 31;
        }

        //判断小月
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            count = 30;
        }

        //判断平年与闰年
        if (month == 2) {
            if (isLeapYear(year)) {
                count = 29;
            } else {
                count = 28;
            }
        }
        return count;
    }

    /**
     * 为月视图初始化日历
     *
     * @param year        year
     * @param month       month
     * @param currentDate currentDate
     * @param weekStar    weekStar
     * @return 为月视图初始化日历项
     */
    public static List<CalendarModel> initCalendarForMonthView(int year, int month, CalendarModel currentDate, int weekStar) {
        java.util.Calendar date = java.util.Calendar.getInstance();

        date.set(year, month - 1, 1);

        int mPreDiff = getMonthViewStartDiff(year, month, weekStar);//获取月视图其实偏移量

        int monthDayCount = getMonthDaysCount(year, month);//获取月份真实天数

        int preYear, preMonth;
        int nextYear, nextMonth;

        int size = 42;

        List<CalendarModel> mItems = new ArrayList<>();

        int preMonthDaysCount;
        if (month == 1) {//如果是1月
            preYear = year - 1;
            preMonth = 12;
            nextYear = year;
            nextMonth = month + 1;
            preMonthDaysCount = mPreDiff == 0 ? 0 : getMonthDaysCount(preYear, preMonth);
        } else if (month == 12) {//如果是12月
            preYear = year;
            preMonth = month - 1;
            nextYear = year + 1;
            nextMonth = 1;
            preMonthDaysCount = mPreDiff == 0 ? 0 : getMonthDaysCount(preYear, preMonth);
        } else {//平常
            preYear = year;
            preMonth = month - 1;
            nextYear = year;
            nextMonth = month + 1;
            preMonthDaysCount = mPreDiff == 0 ? 0 : getMonthDaysCount(preYear, preMonth);
        }
        int nextDay = 1;
        for (int i = 0; i < size; i++) {
            CalendarModel calendarModelDate = new CalendarModel();
            if (i < mPreDiff) {
                calendarModelDate.setYear(preYear);
                calendarModelDate.setMonth(preMonth);
                calendarModelDate.setDay(preMonthDaysCount - mPreDiff + i + 1);
            } else if (i >= monthDayCount + mPreDiff) {
                calendarModelDate.setYear(nextYear);
                calendarModelDate.setMonth(nextMonth);
                calendarModelDate.setDay(nextDay);
                ++nextDay;
            } else {
                calendarModelDate.setYear(year);
                calendarModelDate.setMonth(month);
                calendarModelDate.setCurrentMonth(true);
                calendarModelDate.setDay(i - mPreDiff + 1);
            }
            if (calendarModelDate.equals(currentDate)) {
                calendarModelDate.setCurrentDay(true);
            }
            LunarCalendar.setupLunarCalendar(calendarModelDate);
            mItems.add(calendarModelDate);
        }
        return mItems;
    }

}
